﻿using System;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web.Mvc;
using IFDB.Models.Contexts;
using IFDB.Models.Tables;
using IFDB.ResourceLibrary;
using IFDB.ViewModels.Foundation;
using IFDB.ViewModels.PartialViewModels;
using IFDB.ViewModels.PartialViewModels.Interfaces;

namespace IFDB.ViewModels.PageViewModels
{
    public class LeagueViewModel
    {
        #region Constructor
        public LeagueViewModel()
        {
        }

        public LeagueViewModel(Country country, Guid? competitionId, Guid? seasonId, DateTime? startDate, DateTime? endDate, int page)
        {
            using (IFDBContext db = new IFDBContext())
            {
                short defaultRank = 1;

                this.Country = country;
                this.SeasonId = seasonId ?? db.Seasons.Single(s => s.StartDate <= DateTime.Now && s.EndDate >= DateTime.Now).SeasonId;
                this.StartDate = startDate ?? db.Seasons.Single(s => s.SeasonId == SeasonId).StartDate;
                this.EndDate = endDate ?? db.Seasons.Single(s => s.SeasonId == SeasonId).EndDate;

                this.Competition = competitionId != null
                    ? db.Competitions.Single(c => c.CompetitionId == competitionId)
                    : db.Competitions.First(c => c.Country == country && c.Rank == defaultRank && c.StartDate <= EndDate && c.EndDate >= EndDate);

                this.Page = page;
            }
            
        }
        #endregion

        public Country Country { get; private set; }
        public Competition Competition { get; set; }
        public Guid SeasonId { get; set; }

        [DisplayFormat(DataFormatString = "{0:d MMMM yyyy}", ApplyFormatInEditMode = true)]
        [DataType(DataType.Date)]
        public DateTime StartDate { get; set; }

        [DisplayFormat(DataFormatString = "{0:d MMMM yyyy}", ApplyFormatInEditMode = true)]
        [DataType(DataType.Date)]
        public DateTime EndDate { get; set; }

        public int Page { get; set; }

        private LeagueTableViewModel leagueTableViewModel;
        public LeagueTableViewModel LeagueTableViewModel
        {
            get
            {
                if (leagueTableViewModel == null)
                    leagueTableViewModel = new LeagueTableViewModel(Competition, StartDate, EndDate);

                return leagueTableViewModel;
            }
        }

        private ICompetitionMatchListViewModel competitionMatchListViewModel;
        public ICompetitionMatchListViewModel CompetitionMatchListViewModel
        {
            get
            {
                if (competitionMatchListViewModel == null)
                    competitionMatchListViewModel = new CompetitionMatchListViewModel(Competition, SeasonId, StartDate, EndDate, Page);

                return competitionMatchListViewModel;
            }
        }

        private ICompetitionMatchListViewModel competitionFixtureListViewModel;
        public ICompetitionMatchListViewModel CompetitionFixtureListViewModel
        {
            get
            {
                if (competitionFixtureListViewModel == null)
                    competitionFixtureListViewModel = new CompetitionFixtureListViewModel(Competition, SeasonId, StartDate, EndDate, Page);

                return competitionFixtureListViewModel;
            }
        }


        public SelectList CompetitionList 
        {
            get
            {
                using (IFDBContext db = new IFDBContext())
                    return new SelectList(
                        db.Competitions.Where(c => c.Country == Country && c.StartDate <= StartDate && c.EndDate >= EndDate).OrderBy(c => c.Rank).ToList(), 
                        "CompetitionId", 
                        "ShortName", 
                        Competition.CompetitionId);
            }
        }

        public SelectList SeasonList
        {
            get
            {
                using (IFDBContext db = new IFDBContext())
                    return new SelectList(
                        db.Seasons.Where(s => s.Country == Country).OrderByDescending(s => s.StartDate).ToList(), 
                        "SeasonId", 
                        "SeasonName", 
                        SeasonId);
            }
        }
    }
}
